package com.example.springevent.controller;

import com.example.springevent.model.Order;
import com.example.springevent.model.User;
import com.example.springevent.service.EventDemoService;
import com.example.springevent.service.OrderService;
import com.example.springevent.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/events")
@RequiredArgsConstructor
public class EventDemoController {
    
    private final UserService userService;
    private final OrderService orderService;
    private final EventDemoService eventDemoService;
    
    @PostMapping("/user/register")
    public ResponseEntity<User> registerUser(@RequestBody User user) {
        log.info("接收到用户注册请求: {}", user);
        User registeredUser = userService.registerUser(user);
        return ResponseEntity.ok(registeredUser);
    }
    
    @PostMapping("/order/create")
    public ResponseEntity<Order> createOrder(@RequestParam Long userId, @RequestParam BigDecimal amount) {
        log.info("接收到订单创建请求: userId = {}, amount = {}", userId, amount);
        Order order = orderService.createOrder(userId, amount);
        return ResponseEntity.ok(order);
    }
    
    @PostMapping("/order/process")
    public ResponseEntity<?> processOrder(
            @RequestParam Long userId, 
            @RequestParam BigDecimal amount,
            @RequestParam(defaultValue = "false") boolean shouldFail) {
        log.info("接收到订单处理请求: userId = {}, amount = {}, shouldFail = {}", 
                userId, amount, shouldFail);
        
        try {
            Order order = orderService.processOrderWithTransaction(userId, amount, shouldFail);
            return ResponseEntity.ok(order);
        } catch (Exception e) {
            Map<String, String> response = new HashMap<>();
            response.put("error", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
    
    @PostMapping("/generic")
    public ResponseEntity<Map<String, String>> publishGenericEvent(
            @RequestParam String type, 
            @RequestParam String message) {
        log.info("接收到通用事件发布请求: type = {}, message = {}", type, message);
        eventDemoService.publishGenericEvent(type, message);
        
        Map<String, String> response = new HashMap<>();
        response.put("status", "success");
        response.put("message", "通用事件已发布");
        return ResponseEntity.ok(response);
    }
    
    @PostMapping("/payload")
    public ResponseEntity<Map<String, String>> publishPayloadEvent(
            @RequestParam String name, 
            @RequestParam String payload) {
        log.info("接收到Payload事件发布请求: name = {}, payload = {}", name, payload);
        eventDemoService.publishPayloadEvent(name, payload);
        
        Map<String, String> response = new HashMap<>();
        response.put("status", "success");
        response.put("message", "Payload事件已发布");
        return ResponseEntity.ok(response);
    }
    
    @PostMapping("/async")
    public ResponseEntity<Map<String, String>> publishAsyncEvent(@RequestParam String message) {
        log.info("接收到异步事件发布请求: message = {}", message);
        eventDemoService.publishAsyncEvent(message);
        
        Map<String, String> response = new HashMap<>();
        response.put("status", "success");
        response.put("message", "异步事件已发布");
        return ResponseEntity.ok(response);
    }
}